<template>
	<basic-container>
		<avue-crud :option="option" :data="data" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
		 @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
		 @on-load="onLoad">
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';
	import {
		getCodes
	} from "@/api/system/dict";

	export default {
		data() {
			return {
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					searchMenuSpan: 4,
					border: true,
					index: true,
					refreshBtn: false,
					excelBtn: true,
					menuWidth: '150px',
					column: [{
							label: "编号名称",
							prop: "name",
							search: true,
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "前缀",
							prop: "prefix",
							width: "150px",
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "日期格式",
							prop: "ymd",
							width: "150px",
							type: "select",
							dicData: [],
							props: {
								label: "name",
								value: "name"
							}
						},
						{
							label: "后缀位数",
							type: "number",
							width: "100px",
							prop: "suffix",
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "示例",
							prop: "sample",
							display: false
						},
						{
							label: "当前值",
							prop: "current",
							display: false
						}
					]
				},
				data: []
			};
		},
		created() {
			getCodes('c62f4321-b8dd-43c0-aeb3-77eac695d314').then(res => {
				this.findObject(this.option.column,'ymd').dicData = res.data.data;
			});
		},
		methods: {
			rowSave(row, loading, done) {
				request({
					url: '/api/BaseSerialSetting/add',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
					done();
				}, error => {
					done();
					console.log(error);
				});
			},
			rowUpdate(row, index, loading, done) {
				return request({
					url: '/api/BaseSerialSetting/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
				}, error => {
					done();
					console.log(error);
				});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return request({
							url: '/api/BaseSerialSetting/remove',
							method: 'post',
							params: {
								ids: row.id
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				request({
					url: '/api/BaseSerialSetting/getpages',
					method: 'get',
					params: {
						key: this.query.storeName == undefined ? '' : this.query.storeName,
						type: this.query.storageType == undefined ? '' : this.query.storageType,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.count;
				});
			}
		}
	};
</script>

<style>
</style>
